package com.qj.travel.alivod;

import com.qj.travel.constant.VodConstant;

import javax.crypto.Mac;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
import javax.xml.crypto.dsig.SignatureMethod;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.util.*;

import static com.aliyun.oss.common.utils.DateUtil.formatIso8601Date;
import static com.aliyuncs.auth.AcsURLEncoder.percentEncode;
import org.apache.commons.codec.binary.Base64;

/**
 * Created by Admin on 2016/10/11.
 */
public class RequestParameter {
    final String HTTP_METHOD = "GET";
    Map<String,String> parameterMap = new HashMap<String, String>();
    private static final String ENCODE_TYPE = "UTF-8";

    public StringBuilder returnParam(String action,String mediaIds) throws UnsupportedEncodingException, NoSuchAlgorithmException, InvalidKeyException {
        //加入公共参数
        parameterMap.put("Action",action);
        parameterMap.put("Version","2014-06-18");
        parameterMap.put("AccessKeyId", VodConstant.ACCESS_KEY_ID);
        parameterMap.put("TimeStamp", formatIso8601Date(new Date()));
        parameterMap.put("SignatureMethod", "HMAC-SHA1");
        parameterMap.put("SignatureVersion", "1.0");
        parameterMap.put("SignatureNonce", UUID.randomUUID().toString());
        parameterMap.put("Format", "JSON");

        //加入特有参数
        parameterMap.put("MediaIds", mediaIds);

        //对参数进行排序
        List<String> sortedKeys = new ArrayList<String>(parameterMap.keySet());
        Collections.sort(sortedKeys);

        // 生成stringToSign字符
        final String SEPARATOR = "&";
        final String EQUAL = "=";
        StringBuilder stringToSign = new StringBuilder();
        stringToSign.append(HTTP_METHOD).append(SEPARATOR);
        stringToSign.append(percentEncode("/")).append(SEPARATOR);
        StringBuilder canonicalizedQueryString = new StringBuilder();
        for (String key : sortedKeys) {
            // 此处需要对key和value进行编码
            String value = parameterMap.get(key);
            canonicalizedQueryString.append(SEPARATOR).append(percentEncode(key)).append(EQUAL).append(percentEncode(value));
        }
        // 此处需要对canonicalizedQueryString进行编码
        stringToSign.append(percentEncode(canonicalizedQueryString.toString().substring(1)));

        final String ALGORITHM = "HmacSHA1";
        final String ENCODING = "UTF-8";
        final String secret = VodConstant.ACCESS_KEY_SECRET;//此处请替换成您的AccessKeySecret
        SecretKey key = new SecretKeySpec((secret + SEPARATOR).getBytes(ENCODE_TYPE), SignatureMethod.HMAC_SHA1);
        Mac mac = Mac.getInstance(ALGORITHM);
        mac.init(key);
        String signature = URLEncoder.encode(new String(
                new Base64().encode(mac.doFinal(stringToSign.toString().getBytes(ENCODE_TYPE))),
                ENCODE_TYPE), ENCODE_TYPE);
        // 生成请求URL
        StringBuilder requestURL;
        requestURL = new StringBuilder("http://mts.aliyuncs.com?");
        requestURL.append(URLEncoder.encode("Signature", ENCODE_TYPE)).append("=").append(signature);
        for (Map.Entry<String, String> e : parameterMap.entrySet()) {
            requestURL.append("&").append(percentEncode(e.getKey())).append("=").append(percentEncode(e.getValue()));
        }
        return requestURL;
    }
}
